home *** CD-ROM | disk | FTP | other *** search
- unit Globunit;
- { PC Plus sample Delphi program. Illustrates the usage of global
- variables.
-
- This uses a similar Proc1 procedure to the one in the FuncProc
- project. However, in FuncProc the string variable, 's' is 'local'
- to the Button1Click method and is not 'visible' to other procedures
- unless it is specifically passed as a parameter.
-
- In the current project, 's' is declared 'globally' - at the top of the
- unit and is, therefore, 'visible' to all procedures within the unit.
- Now, when the Proc1 procedure makes s lower-case, s remains
- lower-case when the Button1Click proc displays the string using the
- line:
- Edit2.Text := s;
-
- In general, avoid global variables. They can cause unexpected
- side-effects. }
-
-
-
- interface
-
- uses
- SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
- Forms, Dialogs, StdCtrls;
-
- type
- TForm1 = class(TForm)
- Edit1: TEdit;
- Edit2: TEdit;
- Label1: TLabel;
- Label2: TLabel;
- Button1: TButton;
- procedure Button1Click(Sender: TObject);
- private
- { Private declarations }
- public
- { Public declarations }
- end;
-
- var
- Form1: TForm1;
- s : string; { s is now 'global' }
- implementation
-
- {$R *.DFM}
-
- procedure proc1;
- begin
- s := LowerCase( s );
- end;
-
- procedure TForm1.Button1Click(Sender: TObject);
- begin
- s := Edit1.Text;
- proc1;
- Edit2.Text := s;
- end;
-
-
- end.
-